Skip to content

Conversation

manerlan
Copy link
Member

Closes #71. Continuation of #65.

What

Backend for the profile lists: favourites with a top 4, «Vil prøve» and «Har prøvd», stored per user and public. Also fixes the backend review comments on #65, and adds a demo user for the evaluation.

  • Migration user_wine: one row per user, list and wine, with top_position (1–4) on favourites. Constraints: once per list, positions 1–4 and unique per user, and only favourites can have a position. Indexes for the list pages and for myLists.
  • setWineInList(wineId, list, inList): adds or removes; doing it twice changes nothing. «Har prøvd» takes the wine off «Vil prøve». Returns the wine, so myLists updates in the Apollo cache.
  • setTopFavourites(wineIds): replaces the whole top 4 in the given order ([] clears it). At most four, each once, all favourites; otherwise BAD_USER_INPUT, and the old top 4 is kept. Returns the user.
  • User.wines(list, first, after): newest first, keyset cursors on the row id, so pages neither skip nor repeat when the list changes.
  • User.topFavourites: in the chosen order. A wine taken off the favourites leaves the top 4, and the others keep their order.
  • Wine.myLists: a DataLoader per user, so a page of 24 wines asks for the lists in one query. [] when logged out.
  • saveReview now runs in one transaction and puts the wine on «Har prøvd». Deleting the review keeps it there.
  • Demo user: npm run db:seed:demo creates demo / kork-og-krone with favourites, a top 4, both other lists and five reviews. Running it again resets the user to the same state.

Both mutations call requireUser(); the user always comes from the session, never from an argument.

Review comments from #65 (backend)

  • A wrong foreign key (23503) gave the wrong message. It now checks the constraint name: an unknown wine gives BAD_USER_INPUT, a deleted user gives UNAUTHENTICATED (db-errors.ts).
  • User.reviews could skip or repeat reviews while paging, and a huge after gave a 500. Both now use keyset cursors on the id with a range check (connection.ts), and the index is now (user_id, id DESC). The offset cursor for the wine search also rejects values too large for the database.

The frontend comments from #65 (refetch Me on UNAUTHENTICATED, clearStore() before writeQuery, setWorking(false) in finally) come with the next part #72 .

Commits

  1. fix(api): the right message for each foreign key
  2. fix(api): stable paging for User.reviews, and a range check on cursors
  3. feat(db): the user_wine table
  4. feat(api): setWineInList, User.wines and Wine.myLists
  5. chore(backend): Vitest config as .mts
  6. feat(api): top 4 favourites
  7. feat(api): a saved review marks the wine as tried
  8. feat(db): the demo user
  9. docs: wine lists, the top 4 and the demo user

Tests

  • lists/lists-api.db.test.ts: through Apollo Server against PostgreSQL: login required, the list rules, users kept apart, unknown wines and lists, paging without skipping, one myLists query for a page of 24, the top 4 rules and order, two top 4 changes at once, the database constraints, and reviews marking wines as tried.
  • demo/demo-user.db.test.ts: the documented password works, the data follows the rules, averages are updated, and running it again gives the same state.
  • connection.test.ts, db-errors.test.ts, wine-query.test.ts, review-api.db.test.ts: cursors, foreign key errors, and paging while a review is edited.

Run with the test database (see Database tests):

cd backend
npm run typecheck
TEST_DATABASE_URL=postgresql://vinmonopolet_app:PASSWORD@localhost:5432/vinmonopolet_test npm test

Also tested by hand with curl, following docs/manual-testing.md sections 5 and 6.

How to review

  • Run npm run db:migrate (both your own and the test database), then npm run db:seed:demo.
  • Rules and choices are described in docs/api.md#wine-lists.
  • Try it in Apollo Sandbox at localhost:3001/graphql after logging in as demo.

After merge (VM)

cd backend
npm ci
npm run db:migrate
npm run db:seed:demo

Then restart the backend. Without this, the demo user in the README does not exist on the VM.

Not in this PR

  • The lists in the interface (heart, bookmark, «Har prøvd») and moving the localStorage wishlist to the backend: next part Wine lists in the interface #72 .
  • The profile page: next next part Profile page #73 .
  • The test counts in the README are not updated yet.

@manerlan manerlan requested a review from a team as a code owner November 18, 2026 13:09
@manerlan manerlan linked an issue Nov 18, 2026 that may be closed by this pull request
6 tasks
@manerlan
Copy link
Member Author

One test failed when running npm run check, created a separate issue for it #89

@manerlan
Copy link
Member Author

One test failed when running npm run check, created a separate issue for it #89

Should be fixed in #90
If #90 is merged first, can test by running npm run check

Copy link
Member

@siverne siverne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally with a test database: all 143 tests pass, including the 19 list tests and the demo user. npm run check is green.

This is solid work. A few things I noticed while reading:

Keyset cursors on the row id, with the range check against the largest integer, fixes a real 500 and makes paging stable while a list changes. Good.
FOR NO KEY UPDATE before replacing the top 4 closes a race on the unique index that is easy to miss, and clearing the positions first avoids colliding with the old ones.
Checking the constraint name to tell an unknown wine from a deleted user is much better than guessing from the error code alone.

Two questions, neither blocking:

The rule only goes one way: «Har prøvd» removes the wine from «Vil prøve», but nothing stops putting a tried wine back on «Vil prøve», so it can be on both lists. Is that deliberate? It may well be right, since you can want to taste something again, but worth being a choice rather than an accident.
The demo user is in the README with its password. That makes sense for the evaluation, but on the VM it is a real account anyone with the link can log into and change, and the lists are public. Worth saying in the README that npm run db:seed:demo resets it if a reviewer leaves it in a strange state.

One small thing for the description: it says to run npm run db:migrate for the test database too, but not how. I needed DATABASE_URL=... npx node-pg-migrate up to get there. Worth a line in docs/api.md under Database tests.

@siverne siverne merged commit d32253c into main Nov 19, 2026
@manerlan
Copy link
Member Author

Tested locally with a test database: all 143 tests pass, including the 19 list tests and the demo user. npm run check is green.

This is solid work. A few things I noticed while reading:

Keyset cursors on the row id, with the range check against the largest integer, fixes a real 500 and makes paging stable while a list changes. Good. FOR NO KEY UPDATE before replacing the top 4 closes a race on the unique index that is easy to miss, and clearing the positions first avoids colliding with the old ones. Checking the constraint name to tell an unknown wine from a deleted user is much better than guessing from the error code alone.

Two questions, neither blocking:

The rule only goes one way: «Har prøvd» removes the wine from «Vil prøve», but nothing stops putting a tried wine back on «Vil prøve», so it can be on both lists. Is that deliberate? It may well be right, since you can want to taste something again, but worth being a choice rather than an accident. The demo user is in the README with its password. That makes sense for the evaluation, but on the VM it is a real account anyone with the link can log into and change, and the lists are public. Worth saying in the README that npm run db:seed:demo resets it if a reviewer leaves it in a strange state.

One small thing for the description: it says to run npm run db:migrate for the test database too, but not how. I needed DATABASE_URL=... npx node-pg-migrate up to get there. Worth a line in docs/api.md under Database tests.

Thanks! On the questions:

  1. A deliberate choice: a tried wine can go back on «Vil prøve» (to taste it again, for example another vintage). Only «Har prøvd» moves a wine between the two. It is now a row in the rules table in docs/api.md, and lists-api.db.test.ts tests it.
  2. The README’s demo user section now says plainly that anyone with the link can change it, and that npm run db:seed:demo in backend/ (also on the VM) puts it back as it was.
  3. The command was in docs/api.md under «Database tests», but DATABASE_URL=... npm run db:migrate only works in bash and Git Bash. The section now also has the PowerShell version, and says to run it after every new migration.

Fixed in #72 .

@manerlan manerlan deleted the 71-backend-wine-lists branch November 19, 2026 17:42
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wine lists in the backend (favourites, want to try, tried)
2 participants